package pref.junx.flybird.activity.login;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import pref.junx.flybird.entity.User;
import pref.junx.flybird.entity.User_idea;


/**
 * Created by 半生瓜 on 2017/8/4.
 */

public class LoginPresenter extends LoginContract.Presenter {
    private String userid;
    private String password;
    private String jwt;
    private String user_name;

   // @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override//todo 绑定观察者，网络请求与m层执行，由p层判断，v层响应
    protected void dologin(String un, String psd) {
        rxManager.add(
                modle.getLoginModel(new User(un,psd))
                .subscribe(result -> {
                    if(result.getData().getStuts()==0){
                        view.loginSuccess(result.getData());

                    }else {
                        view.loginFail();
                    }

                        },
                        e -> {
                            Log.i("test2", e.getMessage());
                        }));
    }

    @Override
    public void savaUserInfor(User_idea u) {
        spTools.deleteKey("user_nick");
        spTools.deleteKey("user_name");
        spTools.deleteKey("email");
        spTools.deleteKey("sex");
        spTools.deleteKey("phone");
        spTools.deleteKey("hear");

        spTools.deleteKey("user_id");
        spTools.setString("user_nick",u.getUser_nick());
        spTools.setString("user_name",u.getUser_name());
        spTools.setString("email",u.getEmail());
        spTools.setString("sex",u.getSex());
        spTools.setString("phone",u.getPhone());
        spTools.setString("hear",u.getHear());
        spTools.setInt("user_id",u.getUser_id());
    }

    @Override//todo 保存记录结果，由v层调用该方法
    public void savaRecord(Boolean r, String un, String psd) {
        Log.i("r",String.valueOf(r));

        spTools.setString("jwt", jwt);
        spTools.setString("Name", user_name);
        if (r) {
            spTools.setBoolean("remember", true);
            spTools.setString("username", un);
            spTools.setString("password", psd);



        } else {

            spTools.setBoolean("remember", false);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onCreate(Context context) {

        boolean choseRemember = spTools.getBoolean("remember");

        Log.i("remember读取",String.valueOf(choseRemember));

        //// TODO: 2017/7/19
        if (choseRemember) {
            String username = spTools.getString("username");
            String password = spTools.getString("password");
            view.onCheckBoxResult(choseRemember, username, password);

        }
    }
}

